/**
 * Created by 周树权 on 2017/4/11.
 * 排序
 */
$(document).ready(function () {
    //数组在指定索引位置插入一条记录
    Array.prototype.insert = function (index, item) {
        this.splice(index, 0, item);
    };
    //VUE数据绑定对象
    var addValuesVue = null;
    var pid = app.getUrlParam("ID");
    //定义请求连接
    var callUrl = '';

    addValuesVue = new Vue({
        el: '#dictionarySorting',
        data: {
            myParams: {
                PARACODE: '',//编号
                PARANAME: ''//名称
                //SEQUENCE: ''//callMaxSeq.success ? callMaxSeq.data : ''//次序
            }
        }
    });
    var queryTime = 0;
    var curRow = {};
    var sortGrid = $("#table").bootstrapTable({
        columns: [{
            halign: 'center',
            title: '序号',
            sortable: true,
            formatter: function (value, row, index) {
                row.index = index;
                return index + 1;
            }
        }, {
            field: 'NAME1',
            halign: 'center',
            title: '列名'
        }],
        sortable: false,//是否可以排序，每一列都可以进行指定
        url: "../../../../api/baseCommonqueryController/selectResultBypId",//加载时调用的后台连接
        undefinedText: '-',//字段值为undefine时显示的内容
        striped: true,//是否显示表格线
        iconSize: "outline",//icon图标的显示方式
        toolbar: "#busGrdToolbar",//绑定表格的操作栏，就是新增、修改、删除按钮所在div的ID
        icons: {
            refresh: "glyphicon-repeat",
            toggle: "glyphicon-list-alt",
            columns: "glyphicon-list"
        },
        pagination: false,//是否分页
        sidePagination: "server", // 表示分页时进行服务端请求
        method: 'post',//请求方法[post/get]
        dataField: 'data',
        pageNumber: 1,//页面分页的初始页码
        pageSize: 5,//每页显示数量(暂定每页10条，请暂勿修改，保持统一)
        pageList: [5, 10, 25, 50, 100],//每页显示的可选数量
        queryParamsType: "undefined",

        //查询参数配置
        queryParams: function (params) {
            var pIndex, myTotal;
            if (queryTime === 0) {
                pIndex = 1;
                myTotal = -1;
                queryTime++;
            }
            else {
                pIndex = params.pageNumber;
                myTotal = this.totalRows;
            }
            return {
                limit: params.pageSize,
                page: pIndex,
                allcount: myTotal,
                data: {treeNodeId: pid},
                sortName: params.sortName,
                sortOrder: params.sortOrder
            }
        },
        //排序事件
        onSort: function (name, order) {
            sortGrid.queryData();
        },
        onClickRow: function (row, $element) {
            curRow = row;
            $element.parent("tbody").children("tr").css({
                "background-color": "#f9f9f9",
                "color": "black"
            });
            $element.css({
                "background-color": "#8CD3EC",
                "color": "white"
            });
        },
        //数据加载完毕事件
        onLoadSuccess: function (result) {

        }
    });
    sortGrid.queryData = function () {
        queryTime = 0;
        this.bootstrapTable('refresh', {
            silent: false
        });
    };

    $('#btnDetermine').bind('click', function () {
        var sortList = sortGrid.bootstrapTable('getData');
        if (sortList) {
            for (var i = 0; i < sortList.length; i++) {
                var dicItem = sortList[i];
                dicItem.SEQUENCE = i;
            }
        }
        app.callAjaxPost('../../../../api/baseCommonqueryController/updateResultList', sortList, function (result) {
            if (result.success) {
                //提示是否成功，并且回调关闭当前弹窗
                layer.alert(result.message, {icon: 1}, function (index) {
                    parent.layer.close(parent.layer.getFrameIndex(window.name)); //再执行关闭
                });
            } else {
                layer.alert(result.message, {icon: 5});
            }
        });
    });


    $('#btnUP').bind('click', function () {
        var sortList = sortGrid.bootstrapTable('getData');
        var index = curRow.index;
        if (sortList) {
            if (index && index > 0) {
                sortList.splice(index, 1);
                sortList.insert(index - 1, curRow);
                curRow = sortList[index - 1];
                sortGrid.bootstrapTable('load', sortList);
                var el = '#table > tbody > tr:eq(' + (index - 1).toString() + ')';
                $(el).css({
                    "background-color": "#8CD3EC",
                    "color": "white"
                });
            }
        }
    });

    $('#btnTop').bind('click', function () {
        var sortList = sortGrid.bootstrapTable('getData');
        var index = curRow.index;
        if (sortList) {
            if (index) {
                sortList.splice(index, 1);
                sortList.unshift(curRow);
                curRow = sortList[0];
                sortGrid.bootstrapTable('load', sortList);
                $('#table > tbody > tr:eq(0) ').css({
                    "background-color": "#8CD3EC",
                    "color": "white"
                });

            }
        }

    });
    $('#btnBottom').bind('click', function () {
        var sortList = sortGrid.bootstrapTable('getData');
        var length = sortList.length;
        var index = curRow.index;
        if (sortList) {
            if (index < length - 1) {
                sortList.splice(index, 1);
                sortList.push(curRow);
                curRow = sortList[sortList.length - 1];
                sortGrid.bootstrapTable('load', sortList);
                $('#table > tbody > tr:eq(-1) ').css({
                    "background-color": "#8CD3EC",
                    "color": "white"
                });

            }
        }

    });
    $('#btnDown').bind('click', function () {
        var sortList = sortGrid.bootstrapTable('getData');
        var index = curRow.index;

        if (sortList) {
            if (index < sortList.length - 1) {
                sortList.splice(index, 1);
                sortList.insert(index + 1, curRow);
                curRow = sortList[index + 1];
                sortGrid.bootstrapTable('load', sortList);
                var el = '#table > tbody > tr:eq(' + (index + 1).toString() + ')';
                $(el).css({
                    "background-color": "#8CD3EC",
                    "color": "white"
                });
            }
        }

    });

    var fixHelperModified = function (e, tr) {
            var $originals = tr.children();
            var $helper = tr.clone();
            $helper.children().each(function (index) {
                $(this).width($originals.eq(index).width())
            });
            return $helper;
        },
        updateGrid = function (e, ui) {
            var sortList = sortGrid.bootstrapTable('getData');
            var max = 0;
            var maxOjb = {};
            $(ui.item.parent('tbody').children('tr')).each(function (i) {
                var newIndex = $(this).attr('data-index');
                if (i != newIndex) {
                    var k = Math.abs(i - newIndex);
                    if (max < k) {
                        max = k;
                        maxOjb.i = i;
                        maxOjb.index = newIndex;
                    }
                }
            });
            if (maxOjb.i != maxOjb.index) {
                var obj = sortList[maxOjb.index];
                sortList.splice(maxOjb.index, 1);
                sortList.insert(maxOjb.i, obj);
                sortGrid.bootstrapTable('load', sortList);
            }
            curRow = {};
        };
    $("#table tbody").sortable({
        helper: fixHelperModified,
        stop: updateGrid
    }).disableSelection();
    //排序界面的 返回 按钮
    $('#btnCancel').bind('click', function () {
        //当你在iframe页面关闭自身时
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index); //再执行关闭
    });
});

